package com.itheima.behavior.controller.v1;

import com.itheima.behavior.service.AnalyticsService;
import com.itheima.model.common.dtos.ResponseResult;
import com.itheima.utils.thread.MpThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 数据分析控制器
 */
@Api(tags = "数据分析API")
@RestController
@RequestMapping("/api/v1/analytics")
@Slf4j
public class AnalyticsController {

    @Autowired
    private AnalyticsService analyticsService;

    /**
     * 获取当前用户阅读趋势
     */
    @ApiOperation("获取当前用户阅读趋势")
    @GetMapping("/trend")
    public ResponseResult getMyReadingTrend(
            @ApiParam("天数(7/30)") @RequestParam(defaultValue = "7") Integer days) {
        return analyticsService.getMyReadingTrend(days);
    }

    /**
     * 获取指定用户阅读趋势
     */
    @ApiOperation("获取指定用户阅读趋势")
    @GetMapping("/trend/{userId}")
    public ResponseResult getUserReadingTrend(
            @ApiParam("用户ID") @PathVariable Integer userId,
            @ApiParam("天数(7/30)") @RequestParam(defaultValue = "7") Integer days) {
        return analyticsService.getReadingTrend(userId, days);
    }

    /**
     * 获取图书热度排行
     */
    @ApiOperation("获取图书热度排行")
    @GetMapping("/book/popularity")
    public ResponseResult getBookPopularity(
            @ApiParam("返回数量") @RequestParam(defaultValue = "10") Integer limit,
            @ApiParam("排序字段:view/reading/like/collect/score") 
            @RequestParam(defaultValue = "score") String orderBy) {
        return analyticsService.getBookPopularity(limit, orderBy);
    }

    /**
     * 获取当前用户分类偏好统计
     */
    @ApiOperation("获取当前用户分类偏好统计")
    @GetMapping("/category/my")
    public ResponseResult getMyCategoryStatistics() {
        return analyticsService.getMyCategoryStatistics();
    }

    /**
     * 获取全局分类统计
     */
    @ApiOperation("获取全局分类统计")
    @GetMapping("/category/global")
    public ResponseResult getGlobalCategoryStatistics() {
        return analyticsService.getCategoryStatistics(null);
    }

    /**
     * 获取当前用户活跃度分析
     */
    @ApiOperation("获取当前用户活跃度分析")
    @GetMapping("/activity")
    public ResponseResult getMyActivityAnalysis(
            @ApiParam("统计天数") @RequestParam(defaultValue = "30") Integer days) {
        return analyticsService.getMyActivityAnalysis(days);
    }

    /**
     * 获取当前用户阅读时段分布
     */
    @ApiOperation("获取当前用户阅读时段分布")
    @GetMapping("/time-distribution")
    public ResponseResult getMyReadingTimeDistribution() {
        return analyticsService.getMyReadingTimeDistribution();
    }

    /**
     * 获取平台整体数据概览
     */
    @ApiOperation("获取平台整体数据概览")
    @GetMapping("/platform/overview")
    public ResponseResult getPlatformOverview() {
        return analyticsService.getPlatformOverview();
    }

    /**
     * 获取当前用户阅读报告
     */
    @ApiOperation("获取当前用户阅读报告")
    @GetMapping("/report")
    public ResponseResult getMyReadingReport(
            @ApiParam("报告类型:week/month/year") 
            @RequestParam(defaultValue = "week") String type) {
        return analyticsService.getMyReadingReport(type);
    }

    /**
     * 获取指定用户阅读报告
     */
    @ApiOperation("获取指定用户阅读报告")
    @GetMapping("/report/{userId}")
    public ResponseResult getUserReadingReport(
            @ApiParam("用户ID") @PathVariable Integer userId,
            @ApiParam("报告类型:week/month/year") 
            @RequestParam(defaultValue = "week") String type) {
        return analyticsService.getReadingReport(userId, type);
    }
}

